package com.bolao;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.Adapter.NewsAdapter;
import com.entidades.News;
import com.entidades.Team;

@SuppressLint("ValidFragment")
public class NewsFragment extends Fragment {

	private ListView listView;
	private Team team;
	private static final String WORLDCUP_NEWS_RSS_XML = "/worldcup/news/rss.xml";
	public NewsFragment(List<Team> teamList){
		
	}
	
	public NewsFragment(Team team){
		this.team = team;
	}

	public NewsFragment(){
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.news_fragment, container, false);
		listView = (ListView) rootView.findViewById(R.id.listViewNews);
		
		NewsAdapter news;
		if(team==null) {
			news = new NewsAdapter(getActivity().getApplicationContext(), News.getNewsGeneral(getString(R.string.fifaRSS), WORLDCUP_NEWS_RSS_XML));
		} else {
			news = new NewsAdapter(getActivity().getApplicationContext(), team.getNews());
		}
		listView.setAdapter(news);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int arg2,
					long arg3) {
				
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(((News) adapter.getItemAtPosition(arg2)).getmLink()));
				startActivity(browserIntent);
			}
			
		});
		return rootView;
	}
}
